﻿using System;
using System.Collections.Generic;
using System.Text;


using Gx.Dal.Shared;//数据
using Gx.Model.Entity;//实体
using Gx.Common;
using Gx.Model;
using Gx.Model.Enums;
using Gx.Dal;
using System.Linq;
using Gx.Model.Other;

namespace Gx.Bll.Manage
{
    /// <summary>
    /// 区域信息
    /// </summary>
    public class C_AreaInfoBll
    {
        public ReturnModel<C_AreaInfo> AddC_AreaInfo(C_AreaInfo model)
        {
            ReturnModel<C_AreaInfo> r = new ReturnModel<C_AreaInfo>();
            int res = this.IsExists_AreaName(model.AreaName, model.AreaId);
            if (res > 0)
            {
                r.Code = (int)Code.重复数据;
                r.Msg = "区域名称已存在,请重新录入!";
                return r;
            }
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                if (dal.AddAreaInfo(model))
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Success = true;
                    return r;
                }
                r.Code = (int)Code.新增失败;
                r.Msg = "新增失败";
            }
            return r;
        }

        public ReturnModel<C_AreaInfo> EditC_AreaInfo(C_AreaInfo model)
        {
            ReturnModel<C_AreaInfo> r = new ReturnModel<C_AreaInfo>();
            int res = this.IsExists_AreaName(model.AreaName, model.AreaId);
            if (res > 0)
            {
                r.Code = (int)Code.重复数据;
                r.Msg = "区域名称已存在,请重新录入!";
                return r;
            }
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                if (dal.EditAreaInfo(model))
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Success = true;
                    return r;
                }
                r.Code = (int)Code.编辑失败;
                r.Msg = "编辑失败";
            }
            return r;
        }

        public ReturnModel<C_AreaInfo> DelC_AreaInfo(int AreaId)
        {
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                ReturnModel<C_AreaInfo> r = new ReturnModel<C_AreaInfo>()
                {
                    Code = (int)Code.操作成功,
                    Msg = "操作成功",
                    Success = true,
                };
                if (!dal.DelC_AreaInfo(AreaId))
                {
                    r.Code = (int)Code.删除失败;
                    r.Msg = "删除失败";
                    r.Success = false;
                }
                return r;
            }
        }

        public List<C_AreaInfo> GetAreaInfoList()
        {
            return GetAreaInfoList(string.Empty);
        }
        public List<C_AreaInfo> GetAreaInfoList(int? id)
        {
            StringBuilder strWhere = new StringBuilder();
            if (id == null) id = 0;
            strWhere.AppendFormat(" AND ParentId={0}", id);
            List<C_AreaInfo> areaList = this.GetAreaInfoList(strWhere.ToString());
            areaList.ForEach(x => { x.state = "closed"; });
            return areaList;

        }
        public List<C_AreaInfo> GetAreaInfoList(string strWhere)
        {
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                return dal.GetAreaInfoList(strWhere);
            }
        }

        public List<C_AreaInfo> GetAreaInfoList(int? id, int pageIndex, int pageSize, out int totalCount)
        {
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhere = new StringBuilder();
                if (id == null) id = 0;
                strWhere.AppendFormat(" AND ParentId={0}", id);
                List<C_AreaInfo> areaList = dal.GetAreaInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
                areaList.ForEach(x => { x.state = "closed"; });
                return areaList;
            }
        }

        public ReturnModel<C_AreaInfo> GetAreaInfoModel(int id)
        {
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                ReturnModel<C_AreaInfo> r = new ReturnModel<C_AreaInfo>()
                {
                    Code = (int)Code.操作成功,
                    Success = true,
                    Msg = "请求成功!",
                    Data = dal.GetAreaInfoModel(id),
                };
                return r;
            }
        }

        public List<C_AreaInfo> GetAreaInfoList(int parentId)
        {
            if (parentId == -1)
            {
                return new List<C_AreaInfo>();
            }
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" AND ParentId={0}", parentId);

                List<C_AreaInfo> areaList = new List<C_AreaInfo>();

                areaList.Add(new C_AreaInfo() { AreaId = 0, AreaName = "请选择" });
                areaList.AddRange(dal.GetAreaInfoList(strWhere.ToString()));
                return areaList;
            }
        }

        public int IsExists_AreaName(string areaName, int areaId)
        {
            using (C_AreaInfoDal dal = new C_AreaInfoDal())
            {
                return dal.IsExists_AreaName(areaName, areaId);
            }

        }


        public List<TreeView> GetAreaComboxTree()
        {
            List<TreeView> tvList = new List<TreeView>();
            var list = GetAreaInfoList();
            var parents = list.Where(x => x.ParentId == 0).ToList();
            tvList.Add(new TreeView() { id = 0, text = "顶级", children = new List<TreeView>() });
            TreeView tv = null;
            foreach (var item in parents)
            {
                tv = new TreeView()
                {
                    id = item.AreaId,
                    text = item.AreaName,
                    children = new List<TreeView>()
                };
                GetTreeChilds(list, item, tv);
                tvList[0].children.Add(tv);
            }
            return tvList;
        }

        private void GetTreeChilds(List<C_AreaInfo> list, C_AreaInfo parent, TreeView tv)
        {
            List<C_AreaInfo> newList = list.Where(x => x.ParentId == parent.AreaId).ToList();
            foreach (var item in newList)
            {
                TreeView newTv = new TreeView()
                {
                    id = item.AreaId,
                    text = item.AreaName,
                    children = new List<TreeView>(),
                };
                GetTreeChilds(list, item, newTv);
                tv.children.Add(newTv);
            }
        }
    }
}
